#define PI 3.14

uniform sampler2D   shadowMap;
uniform vec4        lightColor;
uniform vec2        lightDirection;
uniform float       dotLit;

void main() {
    // rectangular to polar coordinates
    // value between [0,1] -> [-1,1]
    vec2 normCoord = gl_TexCoord[0].st * 2.0 - 1.0;
    float lightIntensity = length( normCoord );
    float isLit = 1.0;
    
    if ( dotLit > -1.0 && dot( normalize( normCoord ), lightDirection ) < dotLit ) {
        // this texel isn't lit
        isLit = 0.0;
    } else {
        float theta = atan( normCoord.y, normCoord.x );
        
        // shadow map info is 1D
        // angle value goes like this
        //  -PI   -PI/2    0     PI/2    PI
        //   0    0.25    0.5    0.75     1
        vec2 polarCoord = vec2( ( theta + PI ) / ( 2.0 * PI ), 0.0 );
        
        // get the result lightIntensity
        isLit = step( lightIntensity, texture2D( shadowMap, polarCoord ).r );
    }
    
    gl_FragColor = lightColor * isLit;
    gl_FragColor.a *= (1 - lightIntensity);
}